<template>
	<view class="content">
		<view class="Box">
			<view class="BoxView">
				<view class="BoxOne">
					<u--image :showLoading="true" :src="info.avatar" width="160rpx" height="160rpx" radius="0"
						shape="circle"></u--image>
					<view class="BoxContent">
						<view class="BoxTop marginBottom">
							<text class="title">{{info.name}}</text>
							<text style="color:#888888;margin-left:15rpx;">{{info.country.name}}教师</text>
						</view>
						<view class="BoxTop marginBottom">
							<u--image :showLoading="true" src="../../static/Home/shoucang.png" width="24rpx"
								height="24rpx" radius="0"></u--image>
							<text class="fen">{{info.avg_comment_score}}</text>
						</view>
						<view class="BoxTop">
							<text class="Newtitle">{{info.speciality}}</text>
						</view>
					</view>
				</view>
				<u--image :showLoading="true" src="../../static/Home/heiaixin.png" width="32rpx" height="32rpx"
					radius="0"></u--image>
			</view>
		</view>

		<view class="headers_title">
			<view class="view"></view>
			<text>{{info.speciality}}</text>
		</view>

		<view class="scrollBody">
			<text>{{info.description}}</text>
		</view>

		<view class="headers_title">
			<view class="view"></view>
			<text>工作时间</text>
		</view>

		<view class="working_hours">
			<view class="view">
				<u--image :showLoading="true" src="../../static/Home/rqi.png" width="34rpx" height="34rpx"
					radius="0"></u--image>
				<text>周一至周日</text>
			</view>
			<view class="view">
				<u--image :showLoading="true" src="../../static/Home/shijian.png" width="34rpx" height="34rpx"
					radius="0"></u--image>
				<text>30分钟/节</text>
			</view>
		</view>

		<view class="headers_title">
			<view class="view"></view>
			<text>预约时间</text>
		</view>
		<view class="headers">
			<scroll-view :scroll-top="scrollTop" :scroll-x="true" class="view" v-for="(item,i) in dateArray" :key="i">
				<view>
					<text class="week">{{ item.week }}</text>
					<text :class="[item.is_check ? 'sun1' : 'sun']" @click="getDay(item,i)">{{ item.day }}</text>
				</view>
			</scroll-view>
		</view>
		<view class="button_flex">
			<view v-for="(item,index) in info.times" :key="index">
				<button @click="chooseTime(item.time)"
					style="background: linear-gradient( 270deg, #3078FF 0%, #009DFE 100%);"
					v-if="item.is_booked==0">{{item.time}}</button>
				<button style="background-color:#efefef !important;color: #000;" v-else>{{item.time}}</button>
			</view>
		</view>

		<view class="statusType">
			<view class="leftview">
				<view></view>
				<text>可预约</text>
			</view>
			<view class="rightview">
				<view></view>
				<text>不可预约</text>
			</view>
		</view>

		<button class="Single_appointment" @click="confirm">单次预约</button>
	</view>
</template>

<script>
	import {
		teacherLists,
		collectTeacher,
		teacher_category,
		teacherCountry,
		teacher_info,

	} from '../../api/Teacher.js';
	import {
		courseOneAdd,
	} from '../../api/Course.js';
	import moment from 'moment';
	export default {
		data() {
			return {
				choose_index: 0,
				dateArray: [],
				scrollTop: 0,
				courses: [{
					name: '全部',
					id: 0
				}],
				info: {},
				time: '',
				req: {
					teacher_id: 0,
					shop_id: 0,
					user_vip_id: 0,
					date: '',
					start_time: ''
				}

			}
		},
		onLoad(option) {
			console.log(option)
			this.req.teacher_id = option.id
			this.req.shop_id = uni.getStorageSync('shop_id') || 1;
			this.teacherLists();
			this.teacher_category();
			this.setDate();
			this.teacher_info(option.id, this.req.date);
		},
		onShow() {
			uni.$on('changeInvoiceClose', (data) => {
				this.req.user_vip_id = data.id
				console.log(data.id, 'ffffffffff')
				this.courseOneAdd(this.req);
			})
		},
		methods: {
			generateDateArray() {
				const weekDays = ['日', '一', '二', '三', '四', '五', '六'];
				const startDate = moment(); // 今天的日期
				const endDate = moment().add(6, 'months'); // 半年后的日期

				const dateArray = [];
				let currentDate = startDate;

				while (currentDate.isBefore(endDate) || currentDate.isSame(endDate, 'day')) {
					dateArray.push({
						day: currentDate.date(),
						week: weekDays[currentDate.day()],
						date: currentDate.year() + '-' + currentDate.month() + '-' + currentDate.date(),
						is_check: 0
					});
					currentDate = currentDate.add(1, 'day'); // 增加一天
				}
				dateArray[0].is_check = 1;
				this.dateArray = dateArray;
			},
			chooseTime(e) {
				console.log(e, 'iiii')
				uni.navigateTo({
					url: '/pages/Mine/MyVip'
				})
				this.req.start_time = e
			},
			setDate() {
				var now = new Date();
				var year = now.getFullYear();
				var month = now.getMonth() + 1; // 注意：月份从0开始，需要加1
				var day = now.getDate();
				this.req.date = year + '-' + month + '-' + day;
			},
			getDay(item, i) {
				this.dateArray[this.choose_index].is_check = 0;
				this.dateArray[i].is_check = 1;
				this.choose_index = i;
				this.req.date = item.date
				this.teacher_info(this.req.teacher_id, this.req.date)
				console.log(item);
			},
			/**
			@method 学习语种u-picker确认
			**/
			confirm() {
				if (this.req.start_time == '') {
					uni.showToast({
						title: '请选择预约时间',
						icon: 'none',
						duration: 2000
					});
				}
				try {
					const res = this.courseOneAdd(this.req);
				} catch (err) {
					//TODO handle the exception
					console.log(err);
				}

			},
			/**
			@method 来自国家u-picker确认
			**/
			teacherCountrys_confirm(e) {
				this.req = {
					...this.req,
					...{
						country_name: e.value[0].name || '',
						country_id: e.value[0].id || ''
					}
				};
				this.teacherLists();
				this.teacherCountry_show = false;
			},
			/**
			@method 老师列表
			**/
			async teacherLists() {
				try {
					const res = await teacherLists({
						shop_id: 1,
						page: 1,
						limit: 10
					});
					this.lists = [...res.data.data];
				} catch (err) {
					//TODO handle the exception
					console.log(err);
				}
			},
			/**
			@method 老师分类
			**/
			async teacher_category() {
				try {
					const res = await teacher_category();
					this.columns = [...[res.data]];
				} catch (err) {
					//TODO handle the exception
					console.log(err);
				}
			},
			/**
			@method 老师所属国家
			**/
			async teacher_info(teacher_id, date) {
				try {
					const res = await teacher_info({
						teacher_id: teacher_id,
						date: date
					});
					this.info = {
						...res.data
					};
					console.log(this.info);
				} catch (err) {
					//TODO handle the exception
					console.log(err);
				}
			},
			/**
			@method 老师列表
			**/
			async courseOneAdd(req) {
				try {
					const res = await courseOneAdd(req);
					uni.showToast({
						title: res.code == 1 ? '预约成功' : '预约失败',
						icon: 'none',
						duration: 2000
					});
				} catch (err) {
					//TODO handle the exception
					console.log(err);
				}
			},
			change(e) {
				console.log(e);
			},
			linkUrl(e) {
				uni.navigateTo({
					url: e
				});
			}
		},
		mounted() {
			this.generateDateArray();
		}
	}
</script>

<style>
	page {
		background-color: #f7f7f7;
	}
</style>
<style scoped lang="scss">
	.content {
		width: auto;
		height: 30vh;
		padding: 30rpx;

		background: linear-gradient(182deg, #EDF6FD 0%, #C8EEFD 0%, #f7f7f7 72%, #f7f7f7 100%);
		position: relative;

		.Single_appointment {
			width: 378rpx;
			height: 70rpx;
			margin: 0px auto;
			border-radius: 50rpx;
			background-color: #1f86ff;
			color: #fff;
			display: flex;
			justify-content: center;
			align-items: center;
			font-size: 28rpx;
			letter-spacing: 2rpx;
			margin-top: 100rpx;
		}

		.statusType {
			display: flex;
			align-items: center;
			margin-top: 30rpx;

			.leftview {
				display: flex;
				align-items: center;
				margin-right: 30rpx;

				view {
					width: 62rpx;
					height: 14rpx;
					background-color: #1f86ff;
					border-radius: 50rpx;
				}

				text {
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #333333;
					margin-left: 10rpx;
				}
			}

			.rightview {
				display: flex;
				align-items: center;

				view {
					width: 62rpx;
					height: 14rpx;
					background-color: #eaeaea;
					border-radius: 50rpx;
				}

				text {
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #333333;
					margin-left: 10rpx;
				}
			}
		}

		.button_flex {
			display: flex;
			flex-wrap: wrap;
			align-items: center;
			margin-top: 40rpx;
			//overflow: scroll;
			gap: 40rpx;

			button {
				width: 140rpx;
				height: 50rpx;
				font-family: PingFangSC, PingFang SC;
				font-weight: 500;
				font-size: 26rpx;
				border-radius: 46rpx;
				margin: 0px;
				display: flex;
				justify-content: center;
				align-items: center;
				color: #fff;
			}
		}

		.Box {
			padding-bottom: 30rpx;

			.BoxView {
				width: auto;
				margin: 0px auto;
				// background-color: #fff;
				border-radius: 20rpx;

				display: flex;
				justify-content: space-between;
				align-items: center;
			}

			.BoxOne {
				display: flex;
				justify-content: space-between;
				align-items: center;

				.marginBottom {
					margin-bottom: 10rpx;

					.fen {
						font-family: PingFangSC, PingFang SC;
						font-weight: 600;
						font-size: 28rpx;
						color: #FE7000;
						margin-left: 5rpx;
					}
				}

				.BoxTop {
					margin-left: 20rpx;
					display: flex;
					align-items: center;

					text {
						display: block;
						font-size: 24rpx;
					}

					.title {
						font-family: PingFangSC, PingFang SC;
						font-weight: 600;
						font-size: 32rpx;
						color: #333333;
						font-style: normal;
					}

					.Newtitle {
						font-family: PingFangSC, PingFang SC;
						font-weight: 500;
						font-size: 24rpx;
						color: #666666;
					}
				}
			}

			button {
				width: 154rpx;
				height: 60rpx;
				background: linear-gradient(270deg, #3078FF 0%, #009DFE 100%);
				border-radius: 46rpx;
				margin: 0px;
				display: flex;
				justify-content: center;
				align-items: center;
				color: #fff;
				font-size: 26rpx;
			}
		}

		.tips {
			width: auto;
			background: #FFFFFF;
			border-radius: 16rpx;
			display: flex;
			padding: 30rpx;
			margin-top: 20rpx;

			.title {
				font-family: PingFangSC, PingFang SC;
				font-weight: 500;
				font-size: 24rpx;
				color: #000000;
			}

			.viewFlex {
				width: auto;
				margin-left: 10rpx;

				view {
					display: flex;
					align-items: center;

					text {
						font-family: PingFangSC, PingFang SC;
						font-weight: 500;
						font-size: 24rpx;
						color: #666666;
					}
				}
			}
		}

		.working_hours {
			display: flex;
			align-items: center;

			.view {
				display: flex;
				align-items: center;
				margin-right: 50rpx;
				margin-top: 40rpx;

				/deep/ .u-image {
					margin-right: 10rpx;
				}
			}
		}

		.scrollBody {
			margin-top: 10rpx;

			text {
				font-family: PingFangSC, PingFang SC;
				font-weight: 500;
				font-size: 28rpx;
				color: #333333;
			}
		}


		.headers {
			width: auto;
			border-radius: 20rpx;
			padding: 20rpx 20rpx 30rpx 20rpx;
			background-color: #fff;
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 30rpx;
			box-shadow: 0rpx 6rpx 24rpx 0rpx rgba(218, 218, 218, 0.5);
			overflow: scroll;
			white-space: nowrap;

			.view {
				width: 100%;
				white-space: nowrap;

				.week {
					font-family: PingFangSC, PingFang SC;
					font-weight: 600;
					font-size: 24rpx;
					color: #333333;

				}

				.sun {
					font-family: PingFangSC, PingFang SC;
					font-weight: 600;
					font-size: 32rpx;
					color: #333333;
					margin-top: 10rpx;
				}

				.sun1 {
					background: linear-gradient(270deg, #3078FF 0%, #009DFE 100%);
					font-family: PingFangSC, PingFang SC;
					font-weight: 600;
					font-size: 32rpx;
					color: #333333;
					margin-top: 10rpx;
				}

				view {
					width: 50rpx;
					display: inline-block;
					white-space: nowrap;
					display: flex;
					flex-wrap: wrap;
					justify-content: center;
					// border:1px solid red;
					padding-left: 10rpx;
					padding-right: 10rpx;
					flex-direction: column;

					text {
						display: inline-block;
						text-align: center;

					}
				}
			}
		}

		.NewFlex {
			justify-content: space-between;

			/deep/ .u-image {
				margin-left: 10rpx;
			}

			.time {
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 24rpx;
				color: #4C4C4C;
			}
		}

		.headers_title {
			width: auto;
			margin: 0px auto;
			display: flex;
			align-items: center;
			margin-top: 60rpx;

			.view {
				width: 10rpx;
				height: 34rpx;
				background: #029CFE;
				border-radius: 6rpx;
			}

			.flex {
				display: flex;
				align-items: center;
			}

			text {
				font-family: PingFangSC, PingFang SC;
				font-weight: 600;
				font-size: 36rpx;
				color: #333333;
				margin-left: 10rpx;
			}
		}
	}
</style>